using System;
using System.Collections.Generic;
using System.Text;
using Mpi.Common.Collections;
using Mpi.Common;
using System.ComponentModel;

namespace Xeml.Document
{
    /// <summary>
    /// The abstract bas class for xeml values.
    /// </summary>
    public abstract class ValueBase : INotifyPropertyChanged
    {
        #region Fields
        //protected TimeSpan _timePoint;
        protected string _context;
        protected string _unit;
        protected string _label;



        #endregion

        #region Properties


        /// <summary>
        /// A optional label  for this value.
        /// </summary>
        public string Label
        {
            get { return _label; }
            set
            {
                if (_label != value)
                {
                    _label = value;
                    OnPropertyChanged("Label");
                }
            }
        }

        /// <summary>
        /// The unit symbol for the current value.
        /// </summary>
        public string Unit
        {
            get { return _unit; }
            set
            {
                if (_unit != value)
                {
                    _unit = value;
                    OnPropertyChanged("Unit");
                }
            }
        }

        /// <summary>
        /// The context where this value is assigned.
        /// </summary>
        public string Context
        {
            get { return _context; }
            set
            {
                if (_context != value)
                {
                    _context = value;
                    OnPropertyChanged("Context");
                }
            }
        }

        ///// <summary>
        ///// The time point for a value.
        ///// </summary>
        //public virtual TimeSpan Time
        //{
        //    get
        //    {
        //        //causes site effects -> that why I have removed
        //        //if (this.Collection != null && this.Collection.Owner is StorySplit)
        //        //{
        //        //    return this._timePoint >= (this.Collection.Owner as StorySplit).Time ? _timePoint : (this.Collection.Owner as StorySplit).Time;
        //        //}
        //        return this._timePoint;
        //    }
        //    set
        //    {
        //        if (_timePoint != value)
        //        {
        //            this._timePoint = value;
        //            OnPropertyChanged("Time");
        //            // OnChanged();
        //        }
        //    }
        //}
        #endregion

        #region Constructors
        /// <summary>
        /// The standard constructor.
        /// </summary>
        public ValueBase()
        {
        }

        /// <summary>
        /// A parameterized contructor.
        /// </summary>
        public ValueBase(string context)
            : this()
        {
            this._context = context;
        }

 

        /// <summary>
        /// The copy constructor.
        /// </summary>
        /// <param name="vb">The prototype for the copy process.</param>
        public ValueBase(ValueBase vb)
        {
            //this._timePoint = vb.Time;
            this._unit = vb.Unit;
            this._context = vb.Context;
        }

        /// <summary>
        /// A explicit operator to convert ValueBase-object to string
        /// </summary>
        /// <param name="vb"></param>
        /// <returns></returns>
        public static explicit operator string(ValueBase vb)
        {
            if (vb is DynamicValue)
            {
                StringBuilder sb = new StringBuilder();
                //sb.AppendFormat("{0}@{1}", (vb as Values).Val, TimeSpanParser.OptimizedStringRepresentation(vb.Time,TimeSpanParseOption.DoubleIsDay));
                sb.AppendFormat("{0}", (vb as DynamicValue).Val);
                return sb.ToString();
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                foreach (DynamicValue v in (vb as Cycle).Values)
                {
                    sb.AppendFormat("{0}/", TimeSpanExtension.OptimizedStringRepresentation(v.Time, TimeSpanParseOption.DoubleIsHour));
                    sb2.AppendFormat("{0}/", v.Val);
                }
                sb.Remove(sb.Length - 1, 1);
                sb2.Remove(sb2.Length - 1, 1);
                sb.Append(";");
                sb.Append(sb2);
                //sb.AppendFormat("@{0}", TimeSpanParser.OptimizedStringRepresentation(vb.Time, TimeSpanParseOption.DoubleIsDay));
                return sb.ToString();
            }
        }
        #endregion

     
       



       

     

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion

    }
}
